Bring back im context focus-in/out
authorMatthias Clasen <mclasen@redhat.com>
Thu, 20 Feb 2020 12:30:15 +0000 (07:30 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 21 Feb 2020 05:51:03 +0000 (00:51 -0500)
The key controller still needs to track focus,
in order to emit these signals when required.

gtk/gtkeventcontrollerkey.c

index 79103f407c605a80aa76b8230b1161b9c8f4972b..ea2b4c994d24837b970673cb26aff92ab6756329 100644 (file)
@@ -52,8 +52,7 @@ struct _GtkEventControllerKey
 
   GdkEvent *current_event;
 
-  guint is_focus       : 1;
-  guint contains_focus : 1;
+  gboolean is_focus;
 };
 
 struct _GtkEventControllerKeyClass
@@ -145,6 +144,44 @@ gtk_event_controller_key_handle_event (GtkEventController *controller,
   return handled;
 }
 
+static void
+gtk_event_controller_key_handle_crossing (GtkEventController    *controller,
+                                          const GtkCrossingData *crossing,
+                                          double                 x,
+                                          double                 y)
+{
+  GtkEventControllerKey *key = GTK_EVENT_CONTROLLER_KEY (controller);
+  GtkWidget *widget = gtk_event_controller_get_widget (controller);
+  gboolean start_crossing, end_crossing;
+  gboolean is_focus;
+
+  if (crossing->type != GTK_CROSSING_FOCUS)
+    return;
+
+  start_crossing = crossing->direction == GTK_CROSSING_OUT &&
+                   widget == crossing->old_target;
+  end_crossing = crossing->direction == GTK_CROSSING_IN &&
+                 widget == crossing->new_target;
+
+  if (!start_crossing && !end_crossing)
+    return;
+
+  is_focus = end_crossing;
+
+  if (key->is_focus != is_focus)
+    {
+      key->is_focus = is_focus;
+
+      if (key->im_context)
+        {
+          if (is_focus)
+            gtk_im_context_focus_in (key->im_context);
+          else
+            gtk_im_context_focus_out (key->im_context);
+        }
+    }
+}
+
 static void
 gtk_event_controller_key_class_init (GtkEventControllerKeyClass *klass)
 {
@@ -153,6 +190,7 @@ gtk_event_controller_key_class_init (GtkEventControllerKeyClass *klass)
 
   object_class->finalize = gtk_event_controller_key_finalize;
   controller_class->handle_event = gtk_event_controller_key_handle_event;
+  controller_class->handle_crossing = gtk_event_controller_key_handle_crossing;
 
   /**
    * GtkEventControllerKey::key-pressed: